package hibernate;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import controller.Index;
import pojo.Comment;
import dao.CommentDAO;

@Repository("CommentDAO")
public class HibernateCommentDAO implements CommentDAO {
	
	private SessionFactory sessionFactory;
	private static final Logger logger = Logger.getLogger(Index.class.getName());
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Transactional
	public List<Comment> getListCommentByProductid(String productid) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Comment c where c.products.productid =" + productid);
		return query.list();
	}
	@Transactional
	public boolean addComment(Comment comment) {
		Session session = sessionFactory.getCurrentSession();
        try
        {
 
            session.save(comment);
            logger.info("Save comment to database completed");
            return true;
        }
        catch(HibernateException e)
        {
        	logger.error("Save comment got Error, this is it :",e);
        }
        return false;
	}
}
